<template>
	<view class="classlist">
		<view class="load" v-if="!classList.length && !noData">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="content">
			<navigator :url="`/pages/preview/preview?id=${item._id}`" class="item" v-for="item in classList"
				:key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		<view class="load " v-if="classList.length || noData">
			<uni-load-more :status="noData ? 'noMore' : 'loading'"></uni-load-more>
		</view>
		<view class="safe-area-inset-bottom"></view>
	</view>
</template>
<script setup>
import { onLoad, onReachBottom, onShareAppMessage, onShareTimeline, onUnload } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { ApigetClassList, ApigetHistoryList } from '@/api/apis.js'
import { goHome } from '@/utils/time.js'
// 定义一个空的classList和一个空的queryParams
const classList = ref([])
const noData = ref(false)
const pageName = ref()
const queryParams = {
	pageNum: 1,
	pageSize: 12
}



// 定义一个函数onLoad，用于处理页面加载
onLoad((options) => {
	console.log('options', options)
	// 获取options中的id和name
	const { id, name, type } = options
	if (type) queryParams.type = type
	if (id) queryParams.classid = id
	pageName.value = name
	// 设置页面标题
	uni.setNavigationBarTitle({ title: name })
	// 将name赋值给pageName

	// 将id赋值给queryParams

	getClassList()

})
onUnload(() => {
	uni.removeStorageSync('classList')
})
// 调用getClassList函数
// 定义一个异步函数getClassList，用于获取classList
const getClassList = async () => {
	let res;
	// ((调用ApigetClassList函数，传入classid参数，获取响应
	if (queryParams.classid) res = await ApigetClassList(queryParams)
	if (queryParams.type) res = await ApigetHistoryList(queryParams)
	console.log('res',res)
	// 将响应赋值给classList
	classList.value = [...classList.value, ...res.data]
	uni.setStorageSync("classList", classList.value)
	// 如果响应的长度小于queryParams.pageSize，则将noData设置为true
	if (res.data.length < queryParams.pageSize) noData.value = true
}


// 触底加载更多
onReachBottom(() => {
	if (noData.value) return
	queryParams.pageNum++
	getClassList()
})
// 分享给好友
onShareAppMessage((e) => {
	return {
		title: '壁纸,' + pageName.value,
		path: '/pages/classlist/classlist?id=' + queryParams.classid + '&name=' + pageName.value,

	}
})
// 分享朋友圈
onShareTimeline((e) => {
	return {
		title: '壁纸,' + pageName.value,
		query: 'id=' + queryParams.classid + '&name=' + pageName.value,
	}
})
</script>

<style lang="scss">
.classlist {
	.content {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 5rpx;
		padding: 5rpx;

		.item {
			height: 440rpx;

			image {
				display: block;
				width: 100%;
				height: 100%;
			}
		}
	}
}
</style>